package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.AddItemDAO;
import dao.GetItemDAO;
import databean.ItemBean;
import formbean.AddItemForm;

public class AddItemAction extends Action {

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {

	
		// case the first time come
		if (!ServletFileUpload.isMultipartContent(request)) {
			return "addItem.view";
		}

		// get user input information
		ItemBean item = AddItemForm.parse(request);
		
		// update to ebay
		if (item != null) {
			
			String rs = AddItemDAO.addItem(item);

			if (rs==null) {
				request.setAttribute("error", AddItemDAO.getMessage());
				return "addItem.view";
			} else {
				request.setAttribute("rs", rs);
			}
		}
		
		return "ListItem.do";

		
	}

}
